package net.flyinggroup.reactor;

import lombok.extern.slf4j.Slf4j;
import reactor.core.publisher.Flux;
import reactor.util.function.Tuples;

@Slf4j
public class FibonacciGenerator {
    public static Flux<Long> generate() {
        return Flux.generate(() -> Tuples.of(0L, 1L), (state, sink) -> {
            if (state.getT1() < 0)
                sink.complete();
            else {
                log.info("Before generating:" + state.getT1());
                sink.next(state.getT1());
                log.info("After generating:" + state.getT1());
            }
            return Tuples.of(state.getT2(), state.getT1() + state.getT2());
        });
    }
}